<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app div {
            width: 100px;
            height: 100px;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="color = 'blue'">修改div背景</button>
        <div id="el" v-back="color"></div>
    </div>
    <script src="./js/vue.js"></script>
    <script>
        Vue.directive('back', {
            inserted(el, binding, vnode) {
                // console.log(el, binding);
                el.style.background = binding.value
            },

            update(el, binding, vnode) {
                el.style.background = binding.value
            }
        })
        new Vue({
            el: "#app",
            data: {
                color: 'red'
            },
            directives: {
                // back: {
                //     bind(el, binding, vnode) {
                //         // el 指令绑定所在的元素 bind生命周是第一绑定元素时触发 并且只触发一次
                //         // binding 当指令有值的时候才会有用 因为指令的值会在binding里面
                //         // vnode是对应的虚拟dom
                //         el.style.background = "red"
                //     },
                //     inserted(el, binding, vnode) {

                //     },
                //     update(el, binding, vnode) {

                //     },
                //     componentUpdate(el, binding, vnode) {

                //     },
                //     unbind(el, binding, vnode) {

                //     }
                // }
            }
        })
    </script>
</body>

</html>